package framework.view.components.button
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;

import framework.proxy.ComponentProxy;
import framework.utils.TextUtil;

/**
 * ButtonProxy
 * @author jliang
 * 
 */
public class ButtonProxy extends ComponentProxy
{
    //==========================================================================
    //  Class constants
    //==========================================================================
    private static const OUT_STATE:String = "out";
    private static const OVER_STATE:String = "over";
    private static const DOWN_STATE:String = "down";
    private static const DISABLE_STATE:String = "disable";
    //==========================================================================
    //  Constructor
    //==========================================================================
    public function ButtonProxy()
    {
        super();
    }
    //==========================================================================
    //  Properties
    //==========================================================================
    protected var label:TextField;
    
    protected var _onClick:Function;

    public function get onClick():Function
    {
        return _onClick;
    }

    public function set onClick(value:Function):void
    {
        _onClick = value;
    }
    
    protected var _onMouseOver:Function;
    
    public function get onMouseOver():Function
    {
        return _onMouseOver;
    }
    
    public function set onMouseOver(value:Function):void
    {
        _onMouseOver = value;
    }
    
    protected var _onMouseOut:Function;
    
    public function get onMouseOut():Function
    {
        return _onMouseOut;
    }
    
    public function set onMouseOut(value:Function):void
    {
        _onMouseOut = value;
    }
	
	protected var _enabled:Boolean = true;

	public function get enabled():Boolean
	{
		return _enabled;
	}

	public function set enabled(value:Boolean):void
	{
		_enabled = value;
		skin.buttonMode = skin.mouseEnabled = value;
		
		if (!value) skin.gotoAndStop(DISABLE_STATE);
		else skin.gotoAndStop(OUT_STATE);
	}
	
	public var useCapture:Boolean = false;
    //==========================================================================
    //  Overridden methods: ComponentProxy
    //==========================================================================
    override public function parse(target:MovieClip):void
    {
        super.parse(target);
        
        label = skin.getChildByName("label") as TextField;
        skin.gotoAndStop(OUT_STATE);
        
        skin.mouseChildren = false;
        skin.buttonMode = true;
        
        skin.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler, useCapture);
        skin.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler, useCapture);
        skin.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler, useCapture);
        skin.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler, useCapture);
    }
    
    override public function dispose():void
    {
        if (skin)
        {            
            skin.removeEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler, useCapture);
            skin.removeEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler, useCapture);
            skin.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler), useCapture;
            skin.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler, useCapture);
        }
        onMouseOver = null;
        onMouseOut = null;
        onClick = null;
    }
    //==========================================================================
    //  Public methods
    //==========================================================================
    public function set text(value:String):void
    {
		if (label)
		{
			if (label.embedFonts)
			{
				TextUtil.setText(label, value, font);
			}
			else
			{
				label.text = value;
			}
		}
    }
    
    public function get text():String
    {
        return label ? label.text : "";
    }
	
	protected var _font:String;

	public function get font():String
	{
		return _font;
	}

	public function set font(value:String):void
	{
		_font = value;
	}
    
    public function set embedFont(value:Boolean):void
    {
        if (label)
		{
			label.embedFonts = value;
		}
    }
    
    public function get embedFont():Boolean
    {
        return label ? label.embedFonts : false;
    }
    
    protected function mouseOverHandler(event:Event):void
    {
		if (!enabled) return;
        skin.gotoAndStop(OVER_STATE);
        if (onMouseOver != null)
        {
            onMouseOver();
        }
    }
    
    protected function mouseOutHandler(event:Event):void
    {
		if (!enabled) return;
        skin.gotoAndStop(OUT_STATE);
        if (onMouseOut != null)
        {
            onMouseOut();
        }
    }
    
    protected function mouseDownHandler(event:Event):void
    {
		if (!enabled) return;
        skin.gotoAndStop(DOWN_STATE);
    }
    
    protected function mouseUpHandler(event:Event):void
    {
		if (!enabled) return;
        skin.gotoAndStop(OVER_STATE);
        if (onClick != null)
        {
            onClick();
        }
    }
}
}


